/* This file is part of MyRename
 * Copyright (C) 2005-2009 Kai-Chieh Ku (kjackie@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "stdafx.h"
#include "resource.h"
#include "FileName.h"
#include "preferences.h"
#include "others.h"
#include "language.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define ASSERT_MINMAX(v, m, M) ((v) = max(m, min((M), (v))))

CPreferences::CPreferences(LPCTSTR lpPathName /* = NULL */)
	: CIni(lpPathName), all_ready(false)
{
	SETRC(rcMainWindow, 0, 0, 0, 0)
}

CPreferences::~CPreferences()
{
	SaveSettings();
}

CPreferences thePrefs;

void CPreferences::LoadSettings()
{
//[MyRename]
	uVersion = GetUInt(_T("MyRename"), _T("Version"), VER);
	rcMainWindow = GetRect(_T("MyRename"), _T("rcMainWindow"), rcMainWindow);
	bMaximize = GetBool(_T("MyRename"), _T("Maximize"), FALSE);
	bConfirmRn = GetBool(_T("MyRename"), _T("Confirm"), TRUE);
	bConfirmUd = GetBool(_T("MyRename"), _T("ConfirmUndo"), TRUE);
	bAutoDelThumbs = GetBool(_T("MyRename"), _T("AutoDeleteThumbsDB"), FALSE);
	uLanguage = GetUInt(_T("MyRename"), _T("Language"), FindDefaultLang());
//[FileWnd]
	//dir tree --->
	sLastPath = GetString(_T("FileWnd"), _T("LastPath"));
	//dir tree <---
	bSmartSort = GetBool(_T("FileWnd"), _T("SmartSort"), TRUE);
	uLSpiltterPos = GetUInt(_T("FileWnd"), _T("LeftSpiltterPos"), 30);
	uRSpiltterPos = GetUInt(_T("FileWnd"), _T("RightSpiltterPos"), 65);
	ASSERT_MINMAX(uLSpiltterPos, 10, uRSpiltterPos - 10);
	ASSERT_MINMAX(uRSpiltterPos, uLSpiltterPos + 10, 90);

	//existing filename list --->
	GetUIntArray(_T("FileWnd"), _T("ExtColumnWidth"), &uaExtColWidth);
	GetUIntArray(_T("FileWnd"), _T("ExtColumnOrder"), &uaExtColOrder);
	uExtSortCol = GetUInt(_T("FileWnd"), _T("ExtSortColumn"), 0);
	bExtSortAsc = GetBool(_T("FileWnd"), _T("ExtSortAsc"), TRUE);
	bExtAutoScroll = GetBool(_T("FileWnd"), _T("ExtAutoScroll"), TRUE);
	//existing filename list <---
	//preview filename list --->
	GetUIntArray(_T("FileWnd"), _T("PreColumnWidth"), &uaPreColWidth);
	GetUIntArray(_T("FileWnd"), _T("PreColumnOrder"), &uaPreColOrder);
	//preview filename list <---
//[ControlPanel]
	iFnMode = GetInt(_T("ControlPanel"), _T("FilenameMode"), IDC_FN_NONE);
	ASSERT_MINMAX(iFnMode, IDC_FN_NONE, IDC_FN_AFTER);
	sFnInput = GetString(_T("ControlPanel"), _T("FilenameInput"));

	iExtMode = GetInt(_T("ControlPanel"), _T("ExtensionMode"), IDC_EXT_NONE);
	ASSERT_MINMAX(iExtMode, IDC_EXT_NONE, IDC_EXT_REPLACE);
	sExtInput = GetString(_T("ControlPanel"), _T("ExtensionInput"));

	iCaseMode = GetInt(_T("ControlPanel"), _T("CaseMode"), IDC_CASE_NONE);
	ASSERT_MINMAX(iCaseMode, IDC_CASE_NONE, IDC_CASE_UPF);

	bNumEnable = GetBool(_T("ControlPanel"), _T("NumberEnable"), FALSE);
	iNumStart = GetInt(_T("ControlPanel"), _T("NumberStart"), 1);
	ASSERT_MINMAX(iNumStart, -9999999, 99999999);
	iNumDiff = GetInt(_T("ControlPanel"), _T("NumberDiff"), 1);
	ASSERT_MINMAX(iNumDiff, -9999999, 99999999);
	iNumWidth = GetInt(_T("ControlPanel"), _T("NumberWidth"), 2);
	ASSERT_MINMAX(iNumWidth, 1, 256);

	bRpEnable = GetBool(_T("ControlPanel"), _T("ReplaceEnable"), FALSE);
	bRxEnable = GetBool(_T("ControlPanel"), _T("RXEnable"), FALSE);
	bRpMatchCase = GetBool(_T("ControlPanel"), _T("ReplaceMatchCase"), FALSE);
	sRpFind = GetString(_T("ControlPanel"), _T("ReplaceFind"));
	sRpReplace = GetString(_T("ControlPanel"), _T("ReplaceReplace"));

	bBig5Fix = GetBool(_T("ControlPanel"), _T("Big5Fix"), TRUE);
	bFullToHalf = GetBool(_T("ControlPanel"), _T("Full2Half"), TRUE);
//[Filter]
	bFtFolder = GetBool(_T("Filter"), _T("Folder"), FALSE);
	bFtHidden = GetBool(_T("Filter"), _T("Hidden"), FALSE);
	bFtReadOnly = GetBool(_T("Filter"), _T("ReadOnly"), FALSE);
	bFtSystem = GetBool(_T("Filter"), _T("System"), FALSE);
	sFtMask = GetString(_T("Filter"), _T("Mask"), _T("*.*"));
	fnMask.setFileName(sFtMask, FHRS(bFtFolder, bFtHidden, bFtReadOnly, bFtSystem));
}

void CPreferences::SaveSettings()
{
//[MyRename]
	WriteUInt(_T("MyRename"), _T("Version"), uVersion);
	WriteRect(_T("MyRename"), _T("rcMainWindow"), rcMainWindow);
	WriteBool(_T("MyRename"), _T("Maximize"), bMaximize);
	WriteBool(_T("MyRename"), _T("Confirm"), bConfirmRn);
	WriteBool(_T("MyRename"), _T("ConfirmUndo"), bConfirmUd);
	WriteBool(_T("MyRename"), _T("AutoDeleteThumbsDB"), bAutoDelThumbs);
	WriteUInt(_T("MyRename"), _T("Language"), uLanguage);
//[FileWnd]
	//dir tree --->
	WriteString(_T("FileWnd"), _T("LastPath"), sLastPath);
	//dir tree <---
	WriteBool(_T("FileWnd"), _T("SmartSort"), bSmartSort);
	WriteUInt(_T("FileWnd"), _T("LeftSpiltterPos"), uLSpiltterPos);
	WriteUInt(_T("FileWnd"), _T("RightSpiltterPos"), uRSpiltterPos);
	//existing filename list --->
	WriteUIntArray(_T("FileWnd"), _T("ExtColumnWidth"), &uaExtColWidth);
	WriteUIntArray(_T("FileWnd"), _T("ExtColumnOrder"), &uaExtColOrder);
	WriteUInt(_T("FileWnd"), _T("ExtSortColumn"), uExtSortCol);
	WriteBool(_T("FileWnd"), _T("ExtSortAsc"), bExtSortAsc);
	WriteBool(_T("FileWnd"), _T("ExtAutoScroll"), bExtAutoScroll);
	//existing filename list <---
	//preview filename list --->
	WriteUIntArray(_T("FileWnd"), _T("PreColumnWidth"), &uaPreColWidth);
	WriteUIntArray(_T("FileWnd"), _T("PreColumnOrder"), &uaPreColOrder);
	//preview filename list <---
//[ControlPanel]
	WriteInt(_T("ControlPanel"), _T("FilenameMode"), iFnMode);
	WriteString(_T("ControlPanel"), _T("FilenameInput"), sFnInput);

	WriteInt(_T("ControlPanel"), _T("ExtensionMode"), iExtMode);
	WriteString(_T("ControlPanel"), _T("ExtensionInput"), sExtInput);

	WriteInt(_T("ControlPanel"), _T("CaseMode"), iCaseMode);

	WriteBool(_T("ControlPanel"), _T("NumberEnable"), bNumEnable);
	WriteInt(_T("ControlPanel"), _T("NumberStart"), iNumStart);
	WriteInt(_T("ControlPanel"), _T("NumberDiff"), iNumDiff);
	WriteInt(_T("ControlPanel"), _T("NumberWidth"), iNumWidth);

	WriteBool(_T("ControlPanel"), _T("ReplaceEnable"), bRpEnable);
	WriteBool(_T("ControlPanel"), _T("RXEnable"), bRxEnable);
	WriteBool(_T("ControlPanel"), _T("ReplaceMatchCase"), bRpMatchCase);
	WriteString(_T("ControlPanel"), _T("ReplaceFind"), sRpFind);
	WriteString(_T("ControlPanel"), _T("ReplaceReplace"), sRpReplace);

	WriteBool(_T("ControlPanel"), _T("Big5Fix"), bBig5Fix);
	WriteBool(_T("ControlPanel"), _T("Full2Half"), bFullToHalf);
//[Filter]
	bFtFolder = fnMask.isFolder();
	bFtHidden = fnMask.isHidden();
	bFtReadOnly = fnMask.isReadOnly();
	bFtSystem = fnMask.isSystem();
	sFtMask = fnMask;
	WriteBool(_T("Filter"), _T("Folder"), bFtFolder);
	WriteBool(_T("Filter"), _T("Hidden"), bFtHidden);
	WriteBool(_T("Filter"), _T("ReadOnly"), bFtReadOnly);
	WriteBool(_T("Filter"), _T("System"), bFtSystem);
	WriteString(_T("Filter"), _T("Mask"), sFtMask);
}

INT_PTR CPreferences::UIntArrayToStringArray(CStringArray &sArray, const CUIntArray &uArray)
{
	INT_PTR count = uArray.GetCount(), i;
	CString strTemp;
	sArray.RemoveAll();
	sArray.SetSize(count);
	for (i = 0;i < count;i++) {
		strTemp.Format(_T("%u"), uArray[i]);
		sArray.SetAt(i, strTemp);
	}
	return count;
}

INT_PTR CPreferences::StringArrayToUIntArray(CUIntArray &uArray, const CStringArray &sArray)
{
	INT_PTR count = sArray.GetCount(), i;
	uArray.RemoveAll();
	uArray.SetSize(count);
	for (i = 0;i < count;i++)
		uArray.SetAt(i, _ttol(sArray[i]));
	return count;
}
